package com.tseacher.dao.impl;

import com.tseacher.dao.LinkDao;
import com.tseacher.pojo.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class LinkDaoImpl implements LinkDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Link link) {
        String sql = "insert into tb_link (anchor,description) value (?,?)";
        jdbcTemplate.update(sql,new Object[]{link.getAnchor(),link.getDescription()});
    }

    @Override
    public int selectCount() {
        String sql = "select count(id) from tb_link";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public List<Link> selectPage(Integer cpage, Integer rows) {
        String sql = "select * from tb_link limit ?,?";
        return jdbcTemplate.query(sql,new Object[]{(cpage-1)*rows,rows},new BeanPropertyRowMapper<>(Link.class));
    }

    @Override
    public void deleteLink(Integer id) {
        String sql = "delete from tb_link where id=?";
        jdbcTemplate.update(sql,new Object[]{id});
    }
}
